﻿using Alison.EFCore;
using Alison.EFCore.Dto;
using Alison.EFCore.Entity;
using Alison.Service;
using Alison.Tools.HttpContact.Response;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Alison.API.Controllers
{
    /// <summary>
    /// 用户数据控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")] 
    public class UserController : ControllerBase
    {

        private readonly IUserService _service;
        public UserController(IUserService userService)
        {
            _service = userService;
        }
 
        /// <summary>
        /// 根据用户ID获取用户信息 GET请求
        /// </summary>
        /// <remarks>
        /// 例子:
        /// Get api/User/1
        /// </remarks>
        /// <param name="id">主键ID</param>
        /// <returns>返回用户信息</returns>      
        [HttpGet("{id}")]
        public async Task<ApiResponse> Get(int id) => await _service.Get(id);

        /// <summary>
        /// 这是一个带参数的get请求
        /// </summary>
        /// <remarks>
        /// 例子:
        /// Get api/Values/1
        /// </remarks> 
        /// <returns>测试字符串</returns> 
        /// <response code="201">返回value字符串</response>
        /// <response code="400">如果id为空</response>   
        [HttpPost]
        public async Task<ApiResponse> GetAll([FromBody] UserDto dto) => await _service.GetAll(dto);
         
        /// <summary>
        /// 新增用户
        /// </summary> 
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse> Add([FromBody] UserDto dto) => await _service.Add(dto);

        /// <summary>
        /// 保存用户信息
        /// </summary> 
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse> Save([FromBody] UserDto dto) => await _service.Save(dto);

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <remarks>
        /// 例子:
        /// Get api/Values/1
        /// </remarks>
        /// <param name="id">用户ID</param>
        /// <returns>结果</returns>
        [HttpDelete("{id}")]
        public async Task<ApiResponse> Delete(int id) => await _service.Delete(id);

    }
}
